package com.guet.niit.Login.dao;

import com.guet.niit.Login.pojo.Account;
import com.guet.niit.Login.pojo.User;
import com.guet.niit.Login.pojo.UserCar;
import com.guet.niit.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao{
    //查询common_user表，获取用户信息
    @Override
    public User selectUserInfo(Long userId) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        User login = null;
//        List<User> logins = new ArrayList<>();

        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "select * from common_user where id ="+userId;
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                //获取数据
                long id = rs.getLong("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String photo = rs.getString("photo");
                //封装login对象
                login = new User(id,username,password,photo);
                //装载集合
//                logins.add(login);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
//        return logins;
        return login;
    }

    //修改common_user表的记录，为用户保存图片名
    @Override
    public int userUpload(User user) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "update common_user set photo=? where id =?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,user.getPhoto());
            pstmt.setLong(2, user.getId());
            //执行sql
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
        return 1;
    }

    //查询account表获取用户账户信息
    @Override
    public Account selectAccount(Long userId) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        Account account = null;
//        List<User> logins = new ArrayList<>();

        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "select * from account where id ="+userId;
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                //获取数据
                long id = rs.getLong("id");
                Double balance = rs.getDouble("balance");
                String phone = rs.getString("phone");
                //封装account对象
                account = new Account(id,balance,phone);
                //装载集合
//                logins.add(login);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
//        return logins;
        return account;
    }

    //往account表中插入记录
    @Override
    public void AddAccount(Account account) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "insert into account(id,balance,phone) values (?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setLong(1, account.getId());
            pstmt.setDouble(2,account.getBalance());
            pstmt.setString(3,account.getPhone());
            //执行sql
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
    }

    //修改common_user表中的用户名字段
    @Override
    public int updateUserName(User user) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "update common_user set username=? where id =?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,user.getUsername());
            pstmt.setLong(2, user.getId());
            //执行sql
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
        return 1;
    }

    //修改account表中的电话字段
    @Override
    public int updatePhone(Account account) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "update account set phone=? where id =?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,account.getPhone());
            pstmt.setLong(2, account.getId());
            //执行sql
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
        return 1;
    }


    //查询user_car表获取用户拥有的所有汽车信息
    @Override
    public List<UserCar> selectUserCar(Long userId) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        UserCar userCar = null;
        List<UserCar> userCars = new ArrayList<>();

        try {
            Connection conn = JDBCUtil.createConnection();
            String sql = "select * from user_car where user_id ="+userId;
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                //获取数据
                long id = rs.getLong("user_id");
                long carId = rs.getLong("car_id");
                String carName = rs.getString("car_name");
                String licenseNumber = rs.getString("license_number");
                int carType = rs.getInt("car_type");
                String carPhoto = rs.getString("car_photo");
                //封装login对象
                userCar = new UserCar(id,carId,carName,licenseNumber,carType,carPhoto);
                //装载集合
                userCars.add(userCar);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(connection,statement,resultSet);
        }
        return userCars;
    }

}
